package com.molyfun.parents.activity.organization.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.signup.SignUpEditeStudentInfoActivity;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.bean.SignUpDetailResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.ThreeTabBar;


import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/11/10 0010.
 */

public class SignUpDetailInfoFragment extends BaseFragment implements View.OnClickListener, UIEventListener {

    private String TAG = "SignUpDetailInfoFragment";
    @InjectView(R.id.student_layout)
    ScrollView layout_student;
    @InjectView(R.id.layout_mother_info)
    LinearLayout mLayoutInfo;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.edite_button)
    TextView mEdite_button;
    private int mCurrentCategory;
    private String mDepartid;
    private String mEnrollstudnetid;
    private boolean isPrepared;


    private TextView mInfoName;
    private TextView mInfoSex;
    private TextView mInfoBornAate;
    private TextView mInfoEmbory;
    private TextView mInfoRegisteredResidence;
    private TextView mInfoIdentity;
    private LinearLayout reservation_info_layout;
    private LinearLayout reservation_info;
    private LinearLayout layout_email;
    private LinearLayout layout_reservation_info_email;
    private LinearLayout layout_phone;
    private LinearLayout layout_edit_student_phone;

    private TextView mInfoHomeAddress;

    private SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage;
    private String useraddress;
    private static final String FLAG_STUDENTINFO = "student_info";
    private SignUpDetailResponse.DataBean.StudentenrolpageBean mStudentenrolpage;
    private String mUseraddress;


    public static SignUpDetailInfoFragment newInstance(int category, String departid, String enrollstudnetid,SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage,String useraddress) {
        SignUpDetailInfoFragment studentInfoFragment = new SignUpDetailInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("category", category);
        bundle.putString("departid", departid);
        bundle.putString("enrollstudnetid", enrollstudnetid);
        bundle.putParcelable(FLAG_STUDENTINFO,studentenrolpage);
        bundle.putString("useraddress",useraddress);
        studentInfoFragment.setArguments(bundle);
        return studentInfoFragment;
    }
    public static SignUpDetailInfoFragment newInstance() {
        SignUpDetailInfoFragment studentInfoFragment = new SignUpDetailInfoFragment();
        Bundle bundle = new Bundle();
        studentInfoFragment.setArguments(bundle);
        return studentInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student_info, container, false);
        ButterKnife.inject(this, view);
        isPrepared = true;
        mCurrentCategory = getArguments().getInt("category");


        initView();
        return view;
    }

    private void initView() {
        mInfoName = (TextView) mLayoutInfo.findViewById(R.id.student_info_name);
        mInfoSex = (TextView) mLayoutInfo.findViewById(R.id.student_info_sex);
        mInfoBornAate = (TextView) mLayoutInfo.findViewById(R.id.student_info_born_date);
        mInfoEmbory = (TextView) mLayoutInfo.findViewById(R.id.student_info_embory);
        mInfoRegisteredResidence = (TextView) mLayoutInfo.findViewById(R.id.student_info_registered_permanent_residence);
        mInfoIdentity = (TextView) mLayoutInfo.findViewById(R.id.student_info_identity_card);
        reservation_info_layout = (LinearLayout) mLayoutInfo.findViewById(R.id.reservation_info_layout);
        reservation_info = (LinearLayout) mLayoutInfo.findViewById(R.id.reservation_info);
        layout_email = (LinearLayout) mLayoutInfo.findViewById(R.id.layout_email);
        layout_phone = (LinearLayout) mLayoutInfo.findViewById(R.id.layout_phone);
        reservation_info_layout.setVisibility(View.VISIBLE);
        reservation_info.setVisibility(View.VISIBLE);
        mInfoHomeAddress = (TextView) mLayoutInfo.findViewById(R.id.reservation_info_home_address);
        layout_email.setVisibility(View.GONE);
        layout_phone.setVisibility(View.GONE);

        mDepartid = getArguments().getString("departid");
        mEnrollstudnetid = getArguments().getString("enrollstudnetid");
        mEdite_button.setOnClickListener(this);
        mStudentenrolpage = getArguments().getParcelable(FLAG_STUDENTINFO);
        mUseraddress = getArguments().getString("useraddress");
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_STUDENT_INFO, this);
        if(mStudentenrolpage != null && mUseraddress != null) {
            showStudentInfo(mStudentenrolpage, mUseraddress);
        }
        System.out.println(TAG + "initView()");
    }


    public void onParentTabClick(String departid, String enrollstudnetid) {
        System.out.println(TAG + "onParentTabClick()");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println(TAG + "onStart()");
        mLayoutInfo.setVisibility(View.VISIBLE);
        layout_student.setVisibility(View.VISIBLE);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData(mEnrollstudnetid);
            }
        }, 500);
    }

/*
    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
        System.out.println(TAG + "lazyLoad()");
        mLayoutInfo.setVisibility(View.VISIBLE);
        layout_student.setVisibility(View.VISIBLE);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData(mEnrollstudnetid);
            }
        }, 500);

    }*/

    private void showStudentInfo(SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage, String userAddress) {
        mInfoName.setText(studentenrolpage.getName());
        if (studentenrolpage.getSex().equals("1")) {
            mInfoSex.setText("女");
        } else {
            mInfoSex.setText("男");
        }
        mInfoBornAate.setText(TimeUtils.longToString(studentenrolpage.getBirthdatetime(), "yyyy-MM-dd"));
        mInfoEmbory.setText(studentenrolpage.getParity());
        mInfoRegisteredResidence.setText(studentenrolpage.getCityname());
        mInfoIdentity.setText(studentenrolpage.getCardid());

        mInfoHomeAddress.setText(userAddress);
    }

    private void loadData(final String enrollstudnetid) {
        showProgressDialog();
        Call<SignUpDetailResponse> signUpDetailResponseCall = MFPApplication.getInstance().getRestClient().signUpDetailInfo(enrollstudnetid);
        signUpDetailResponseCall.enqueue(new Callback<SignUpDetailResponse>() {
            @Override
            public void onResponse(Call<SignUpDetailResponse> call, Response<SignUpDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    studentenrolpage = response.body().getData().getStudentenrolpage();
                    useraddress = response.body().getData().getUseraddress();
                    if (studentenrolpage != null) {
                        mEdite_button.setVisibility(View.VISIBLE);
                        layout_student.setVisibility(View.VISIBLE);
                        mEmptyLayout.setVisibility(View.GONE);
                        showStudentInfo(studentenrolpage, useraddress);
                    } else {
                        mEdite_button.setVisibility(View.GONE);
                        layout_student.setVisibility(View.GONE);
                        mEmptyLayout.setVisibility(View.VISIBLE);
                    }
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                    mEdite_button.setVisibility(View.GONE);
                    layout_student.setVisibility(View.GONE);
                    mEmptyLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<SignUpDetailResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("数据请求失败");
                mEdite_button.setVisibility(View.GONE);
                layout_student.setVisibility(View.GONE);
                mEmptyLayout.setVisibility(View.VISIBLE);
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edite_button:
                SignUpEditeStudentInfoActivity.startActivity(getActivity(), ThreeTabBar.ThreeTabChooseTab.BAR_ONE, studentenrolpage, useraddress);

                break;
        }
    }

    @Override
    public void handleUIEvent(Message msg) {
        if (msg.what == EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_STUDENT_INFO) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    onStart();
                }
            }, 500);

        }
    }

    private Handler mHandler = new Handler();
}
